<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar></CustomNavbar>
  <!-- 下拉刷新 -->
  <scroll-view
    scroll-y
    class="scroll"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <!-- 轮播图组件 -->
    <XtxSwiper :list="bannerList"></XtxSwiper>
    <!-- 首页分类 -->
    <CategoryPanel :list="categoryList"></CategoryPanel>
    <!-- 热门推荐 -->
    <HotPanel :list="hotItemList"></HotPanel>
    <!-- 猜你喜欢 -->
    <XtxGuess ref="xtxguessRef"></XtxGuess>
  </scroll-view>
</template>

<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/apis/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'

//轮播图
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  bannerList.value = res.result
  // console.log(bannerList.value)
}

//首页分类
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  // console.log(res);
  categoryList.value = res.result
}

//热门推荐
const hotItemList = ref<HotItem[]>([])
const gethotItemData = async () => {
  const res = await getHomeHotAPI()
  // console.log(res);
  hotItemList.value = res.result
}

//加载中标记
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), gethotItemData()]) // 关闭动画
  // getHomeBannerData()
  // getHomeCategoryData()
  // gethotItemData()
  isLoading.value = false
})

//

import type { XtxGuessType } from '@/types/components'
// 给添加类型标准
const xtxguessRef = ref<XtxGuessType>()
// 滚动到底部，会触发 scrolltolower 事件
// 触发 XtxGuess 组件里面的获取数据的方法 父组件需要调用子组件里面的方法  Vue3的语法
const onScrolltolower = () => {
  // console.log('触底了....')
  // 调用了 XtxGuess 组件里面 获取数据的方法
  xtxguessRef.value?.getMore()
}

// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  //promise 同时加载
  xtxguessRef.value?.getMore()
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), gethotItemData()]) // 关闭动画
  isTriggered.value = false
}
</script>



<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll {
  flex: 1;
  overflow: hidden;
}
</style>